package com.liufei1988.spring.overview.dependency.injection;

import com.liufei1988.spring.overview.repository.UserRepository;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;

public class DependencyInjectionDemo {
    public static void main(String[] args){
        BeanFactory beanFactory=new ClassPathXmlApplicationContext("META-INF/dependency-injection-context.xml");

        // 依赖来源一：自定义 Bean
        UserRepository userRepository = beanFactory.getBean("userRepository", UserRepository.class);
        System.out.println(userRepository.getUsers());
        System.out.println(userRepository.getBeanFactory()==beanFactory);
        //System.out.println(userRepository.getBeanFactory())是个内建对象;
        //依赖注入
        System.out.println(userRepository.getBeanFactory());
        //依赖查找（错误代码）
//        System.out.println(beanFactory.getBean(BeanFactory.class));

        ObjectFactory userFactory=userRepository.getUserObjectFactory();
        System.out.println(userFactory.getObject());

        // 依赖来源三：容器內建 Bean
        Environment environment = beanFactory.getBean(Environment.class);
        System.out.println("获取 Environment 类型的 Bean：" + environment);
    }

    private static void whoIsIoCContainer(UserRepository userRepository, ApplicationContext applicationContext) {


        // ConfigurableApplicationContext <- ApplicationContext <- BeanFactory

        // ConfigurableApplicationContext#getBeanFactory()


        // 这个表达式为什么不会成立
        System.out.println(userRepository.getBeanFactory() == applicationContext);

        // ApplicationContext is BeanFactory

    }
}
